package com.atguigu.mobileplayer2.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.atguigu.mobileplayer2.domain.Lyric;
import com.atguigu.mobileplayer2.utils.DensityUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/11/11.
 */

public class MyLyricView1 extends TextView {

    // 歌词类
    private ArrayList<Lyric> lyrics;
    private Paint paint;
    private Paint whitepaint;
    private int width;
    private int height;
    //歌词列表中的索引，是第几句歌词
    private int index=6;
    //  每行的高
    private float textHeight ;
    // 当前歌词内容
    private String content;
    // 时间戳
    private long timePoint;
    // 休眠时间或者高亮显示的时间
    private long sleepTime;
    // 当前播放位置
    private float currentPositon;


    public ArrayList<Lyric> getLyrics() {
        return lyrics;
    }

    public void setLyrics(ArrayList<Lyric> lyrics) {
        this.lyrics = lyrics;
    }

    public MyLyricView1(Context context) {
        this(context,null);
    }

    public MyLyricView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyLyricView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }



    public void setshowNextLyric(int currentPosition) {
//        this.currentPositon= currentPosition;
//        if(lyrics == null ||  lyrics.size() == 0 ){
//            return;
//        }
//        for (int i = 1; i < lyrics.size(); i++) {
//
//            if(currentPosition < lyrics.get(i).getTimePoint()){
//
//                int tempIndex = i - 1;
//
//                if(currentPosition >= lyrics.get(tempIndex).getTimePoint()){
//                    //当前正在播放的哪句歌词
//                    index = tempIndex;
//                    sleepTime = lyrics.get(index).getSleepTime();
//                    timePoint = lyrics.get(index).getTimePoint();
//                }
//
//            }
//        }
        currentPosition++;
        this.index=currentPosition;


        //重新绘制
        invalidate();//在主线程中
        //子线程
//        postInvalidate();
    }

    private void initView(Context context) {
        textHeight = DensityUtil.dip2px(context,18);//对应的像数
        //创建画笔 画中间内容
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setTextSize(DensityUtil.dip2px(context,16));
        paint.setAntiAlias(true);
        //设置居中对齐，画出来的内容会在View中居中
        paint.setTextAlign(Paint.Align.CENTER);
        // 画上下内容
        whitepaint = new Paint();
        whitepaint.setColor(Color.WHITE);
        whitepaint.setTextSize(DensityUtil.dip2px(context,16));
        whitepaint.setAntiAlias(true);
        //设置居中对齐
        whitepaint.setTextAlign(Paint.Align.CENTER);
        // 测试代码，假的歌词
        lyrics = new ArrayList<>();
        Lyric lyric = new Lyric();
        for (int i = 0; i < 1000; i++) {

            // 歌词对应胡时间戳：时间戳
            lyric.setTimePoint(1000 * i);
            // 休眠时间或者高亮显示的时间
            lyric.setSleepTime(1500 + i);
            lyric.setContent(i + "aaaaaaaaaaaaaaa" + i);
            //把歌词添加到集合中
            lyrics.add(lyric);
            lyric = new Lyric();
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(lyrics != null && lyrics.size() >0 ){
            //1. 绘制歌词:绘制当前句
            String currentText = lyrics.get(index).getContent();
            canvas.drawText(currentText, width / 2, height / 2, paint);
            // 2. 绘制前面部分
            float tempY = height / 2;//Y轴的中间坐标
            for (int i = index - 1; i >= 0; i--) {
                //每一句歌词
                String preContent = lyrics.get(i).getContent();
                tempY = tempY - textHeight;
                if (tempY < 0) {
                    break;
                }
                /****
                 *   paint.setTextAlign(Paint.Align.CENTER); 设置以后
                 *   绘制文本 坐标基于 context 的中心点
                 */
                canvas.drawText(preContent, width / 2, tempY, whitepaint);
            }
            // 3. 绘制后面部分
            tempY = height / 2;//Y轴的中间坐标
            for (int i = index + 1; i < lyrics.size(); i++) {
                //每一句歌词
                String nextContent = lyrics.get(i).getContent();
                tempY = tempY + textHeight;
                if (tempY > height) {
                    break;
                }
                canvas.drawText(nextContent, width / 2, tempY, whitepaint);
            }
        }else{
            //没有歌词
            canvas.drawText("没有歌词", width / 2, height / 2, paint);
        }
    }


}